<?php
namespace app\admin\controller;

use app\model\CategoryModel;

class Category{

    //增加数据
    public function create(){
        $category = CategoryModel::create([
            'category_name' => 'thinkphp',
            'category_desc' => 'thinkphp学习',
            'update_time' => time(),
            'add_time' => time()
        ]);

        echo $category -> category_name;
    }

    //查询总的数据
    public function selete(){
        $list = CategoryModel::select();
        foreach($list as $item ){
            echo $item -> category_name;
            echo "<br />";

        }
    }

    //根据条件来进行查询
    public function status(){
        //根据2查询一条数据
//        $list = CategoryModel::where('category_id',2)->find();

        //筛选出所有分类，按照category_id 降序排序
        $list = CategoryModel::where('category_id','<=',8)->order('category_id','desc')->limit(2)->select();
//        var_dump($list);
        foreach ($list as $item){
            echo $item['category_id'];
            echo "<br />";
            echo $item['category_name'];
            echo "<br />";
            echo "<br />";
        }
    }

    ////        示例2，查询id为1、2、3的分类，并打印：
    public function select(){

//        $categoryLIST = CategoryModel::select([2,7]);
        $categoryLIST = CategoryModel::where('category_id','<',8)->limit(2)->select();
//        var_dump($categoryLIST->category_name);
        foreach ($categoryLIST as $item){

            echo $item['category_id'];
            echo "<br />";
            echo $item['category_name'];
            echo "<br />";
            echo $item['category_desc'];
            echo "<br />";
            echo "<br />";
            echo "<br />";
        }
    }


    //获取某个字段或者某个列的值

    public function value(){
        //某个字段的值
//        $categoryList = CategoryModel::where('category_id',7)->value('category_name');
//        var_dump($categoryList);
//        echo $categoryList;
        //某个列的值
        $categoryList = CategoryModel::where('category_id',2)->column('category_name','category_id');
//        $categoryList = CategoryModel::
        var_dump($categoryList);
    }

    // 根据name字段查询用户
    public function name(){

//        $user = CategoryModel::getBycategory_name('css');
//        var_dump($user);
        //示例1，查询分类名称为mysql的分类，并打印：

        $user = CategoryModel::getByCategoryName("css");
        var_dump($user);
        echo $user['category_id'];
        echo $user->category_name;
        echo $user->category_desc;
    }

    //聚合查询
    public function count(){
        //查总的条数
//        $categoryList = CategoryModel::count();

        //根据条件来筛选
//        $categoryList = CategoryModel::where('category_id','>',2)->count();

        //查询平均数
//        $categoryList = CategoryModel::avg("category_id");

        //查询最大值

        $categoryList = CategoryModel::max('category_name',false);
        //字段不是int时需要加多个参数，并返回最大字节的字段数据
        var_dump($categoryList);
    }

    //更新
    public function save(){
        //更新修改
//        $category = CategoryModel::find(2);//查出2的数据
//        $category['category_name'] = 'thinkphp22';//设置名称
//        $category['category_desc'] = 'thinkphp33'; //设置内容
//        $category->save();  //保存到数据库
        //修改
//        CategoryModel::update(['category_name' => 'thinkphp88'],['category_id' =>2]);

        //批量更新
        $array = [
            ['category_id' => 2,
                'category_name' => 'thinkphp111',
                'category_desc' =>'thinkphp222'],
            ['category_id' => 7,
                'category_name' => 'thinkphp111',
                'category_desc' =>'thinkphp222']
        ];

        $user = new CategoryModel();
        $user->saveAll($array);

    }

    //删除数据
    public function delete(){
        //根据编号删除
//        $user = CategoryModel::find(2);
//        $user->delete();
        //批量删除
//        CategoryModel::destroy([7,8,9]);
        //条件删除
//        CategoryModel::where('category_id',"<",12)->delete();
        //根据语句来查询
//        $result = \think\facade\Db::query('select * from category');
//        dump($result);

    }

}